string allchar = ("0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"); string[] cutallchar = allchar.Split(',');//以“,”分割字符串(allchar) string[] myfont = { "微软炭黑", "仿宋", "幼圆", "宋体", "隶书" };//为随机选中某一项做准备 Color[] colors = { Color.Black, Color.Yellow, Color.Blue, Color.Purple, Color.Pink };//为随机选中某一项做准备 Bitmap mybmp = new Bitmap(200, 50);//新建一个位图,大小为(200,50) Graphics mygps = Graphics.FromImage(mybmp);//新建一个Graphics类,里面有很多画图方法 Random random = new Random();//新建随机数生成器 Point point = new Point();//新建坐标点 for (int i = 0; i < 5; i )//开始画所选中的5个字符 { int empt1 = random.Next(0, 62); record = cutallchar[empt1]; point.X = i * 30; point.Y = random.Next(0, 5); mygps.DrawString(cutallchar[empt1], new Font(myfont[random.Next(0, 4)], 30, FontStyle.Regular), new SolidBrush(colors[random.Next(0, 4)]), point); //Graphics.DrawString Graphics类中画字符的方法:DrawString(String^, Font^, Brush^, PointF)在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。 } Point pointA = new Point();//开始画无规则的线,方法大致和上面一样 Point pointB = new Point(); for (int i = 0; i < 15; i ) { pointA.X = random.Next(0, mybmp.Width); pointA.Y = random.Next(0, mybmp.Height); pointB.X = random.Next(0, mybmp.Width); pointB.Y = random.Next(0, mybmp.Height); mygps.DrawLine(new Pen(colors[random.Next(0, 4)]), pointA, pointB); } Point pointt = new Point();//开始无规则画300个像素点 在我们新建的mybmp位图上 for (int i = 0; i < 300; i ) { pointt.X = random.Next(0, mybmp.Width); pointt.Y = random.Next(0, mybmp.Height); mybmp.SetPixel(pointt.X, pointt.Y, colors[random.Next(0, 4)]); } pictureBox1.Image = mybmp;
评论